<?php
$this->beginBlock('toolbar');
$t = $this->toolbar();
echo $t->renderBegin();
echo $t->renderItem($this->link($this->__('system.button.back'), $this->historyUrl('*/*/*/index')));
echo $t->renderEnd();

$this->nextBlock('form');
$f = $this->form()->setId('form')->setAction($this->buildUrl('*/*/*/save'));
if($this->model):
	$f->setData($this->model->getAllData());
endif;

echo $f->renderBegin();
echo $f->createField($this->__('system.label.name'), TRUE)
       ->setInput($this->textbox('name', 'model[name]')->addCharacterRule());
echo $f->createField($this->__('system.label.property_type'), TRUE)
       ->setInput($this->select('property_type', 'model[property_type]')
             ->setOption('--', '')
             ->setOption($this->__('system.label.string'), 'string')
             ->setOption($this->__('system.label.integer'), 'integer')
             ->setOption($this->__('system.label.float'), 'float')
             ->setOption($this->__('system.label.boolean'), 'boolean')
             ->setOption($this->__('system.label.datetime'), 'datetime')
             ->setOption($this->__('system.label.timestamp'), 'timestamp')
             ->setOption($this->__('system.label.email'), 'email')
             ->setOption($this->__('system.label.array'), 'array'));
echo $f->createField($this->__('system.label.item_count'), FALSE, 'field_ic')->setStyle('display', 'none')
       ->setInput($this->textbox('item_count', 'model[item_count]')->addDigitsRule());
echo $f->createField($this->__('system.label.field_type'), TRUE)
       ->setInput($this->select('field_type', 'model[field_type]')
             ->setOption('--', '')
             ->setOption('varchar', 'varchar')
             ->setOption('text', 'text')
             ->setOption('char', 'char')
             ->setOption('int', 'int')
             ->setOption('tinyint', 'tinyint')
             ->setOption('smallint', 'smallint')
             ->setOption('mediumint', 'mediumint')
             ->setOption('bigint', 'bigint')
             ->setOption('float', 'float')
             ->setOption('datetime', 'datetime')
             ->setOption('timestamp', 'timestamp'));
echo $f->createField($this->__('system.label.timestamp_action'), FALSE, 'file_ctime')
       ->setInput($this->select('timestamp_action', 'model[timestamp_action]')
               ->setOption('--', '')
               ->setOption($this->__('system.label.auto_insert_time'), 'auto_insert_time')
               ->setOption($this->__('system.label.auto_update_time'), 'auto_update_time'));
echo $f->createField($this->__('system.label.length'))
       ->setInput($this->textbox('length', 'model[length]')->addDigitsRule());
echo $f->createField($this->__('system.label.insertable'), TRUE)
       ->setInput($this->selectBoolean('insertable', 'model[insertable]')->setValue(TRUE));
echo $f->createField($this->__('system.label.updateable'), TRUE)
       ->setInput($this->selectBoolean('updateable', 'model[updateable]')->setValue(TRUE));
echo $f->createField($this->__('system.label.required'), TRUE)
       ->setInput($this->selectBoolean('required', 'model[required]'));
echo $f->createField($this->__('system.label.primary_key'), TRUE)
       ->setInput($this->selectBoolean('is_primary', 'model[is_primary]'));
echo $f->createField($this->__('system.label.increment'), TRUE)
       ->setInput($this->selectBoolean('is_increment', 'model[is_increment]'));
if($this->router->getAction() == 'create'):
echo $f->createField($this->__('system.label.versionable'))
       ->setInput($this->selectBoolean('versionable', 'model[versionable]'));
endif;
echo $f->createField($this->__('system.label.default_value', FALSE, 'file_dvalue'))
       ->setInput($this->textbox('default_value', 'model[default_value]'));
echo $f->createField($this->__('system.label.input_type'))
       ->setInput($this->select('input_type', 'model[input_type]')
             ->setOption('--', '')
             ->setOption($this->__('system.label.textbox'), 'textbox')
             ->setOption($this->__('system.label.textarea'), 'textarea')
             ->setOption($this->__('system.label.boolean'), 'boolean')
             ->setOption($this->__('system.label.datetime'), 'datetime')
             ->setOption($this->__('system.label.email'), 'email')
             ->setOption($this->__('system.label.select'), 'select')
             ->setOption($this->__('system.label.treeselect'), 'treeselect')
             ->setOption($this->__('system.label.editor'), 'editor')
             ->setOption($this->__('system.label.image_upload'), 'imageupload')
             ->setOption($this->__('system.label.hidden'), 'hidden'));
echo $f->createField($this->__('system.label.label_text'))
           ->setInput(array(
               $this->select('label_select', 'label_select]')
                    ->setOption('--', '')
                    ->setOption($this->__('system.label.use_existe_key'), '1')
                    ->setOption($this->__('system.label.new_existe_key'), '2')
                    ->setStyle('width', 150),
               $this->textbox('label', 'model[label]')
                    ->addRequiredRule("checkLabel")
                    ->setStyle('width', 150),
               $this->textbox('label_text', 'label_text')
                    ->addRequiredRule("checkLabelText")
                    ->setStyle('width', 150)
                    ->setStyle('display', 'none'))
           );
echo $f->createField($this->__('system.label.desc_text'))
           ->setInput(array(
               $this->select('desc_select', '')
                    ->setOption('--', '')
                    ->setOption($this->__('system.label.use_existe_key'), '1')
                    ->setOption($this->__('system.label.new_existe_key'), '2')
                    ->setStyle('width', 150),
               $this->textbox('description', 'model[description]')
                    ->addRequiredRule("checkDesc")
                    ->setStyle('width', 150),
               $this->textbox('description_text', 'description_text')
                    ->addRequiredRule("checkDescText")
                    ->setStyle('width', 150)
                    ->setStyle('display', 'none'))
           );
echo $f->bind($this->hidden('id', 'model[id]'));
echo $this->hidden('entity_id', 'model[entity_id]')->setValue($this->entityId);
echo $f->renderEnd();

$t2 = $this->toolbar();
echo $t2->renderBegin();
echo $t2->renderItem($this->linkButton($this->__('system.button.save'), "submitForm('form')"));
echo $t2->renderEnd();
?>
<script language="javascript">
$('#property_type').change(changePropertyType);
$('#label_select').change(function(){
    if($(this).val() == '2'){
        $('#label_text').show();
    }else{
        $('#label_text').hide();
    }
});
$('#desc_select').change(function(){
    if($(this).val() == '2'){
        $('#desc_text').show();
    }else{
        $('#desc_text').hide();
    }
});

function changePropertyType(){
    $('#field_ic,#file_ctime').hide();
    $('#length').val('');
    $('#default_value').val('');
    var v = $('#property_type').val();
    switch(v){
        case 'string':
            $('#field_type option[value="varchar"]').attr('selected', 'selected');
            $('#length').val('50');
            $('#input_type option[value="textbox"]').attr('selected', 'selected');   
            break;
        case 'array':
            $('#field_ic').show();
            $('#field_type option[value="varchar"]').attr('selected', 'selected');
            $('#length').val('50');
            $('#input_type option[value="multidropdown"]').attr('selected', 'selected');          
            break;
        case 'email':
            $('#field_type option[value="varchar"]').attr('selected', 'selected');
            $('#length').val('50');
            $('#input_type option[value="email"]').attr('selected', 'selected');
            break;
        case 'integer':
            $('#field_type option[value="int"]').attr('selected', 'selected');            
            $('#input_type option[value="dropdown"]').attr('selected', 'selected');
            break;
        case 'float':
            $('#field_type option[value="float"]').attr('selected', 'selected');           
            $('#input_type option[value="textbox"]').attr('selected', 'selected');
            break;            
        case 'boolean':
            $('#field_type option[value="tinyint"]').attr('selected', 'selected');        
            $('#length').val('1');
            $('#input_type option[value="boolean"]').attr('selected', 'selected');
            break;
        case 'datetime':
            $('#field_type option[value="datetime"]').attr('selected', 'selected');
            $('#input_type option[value="datetime"]').attr('selected', 'selected');
            $('#default_value').val('');
            $('#file_dvalue').hide();
            break;  
        case 'timestamp':
            $('#field_type option[value="timestamp"]').attr('selected', 'selected');
            $('#insertable option[value="0"]').attr('selected', 'selected');
            $('#updateable option[value="0"]').attr('selected', 'selected');
            $('#default_value').val('');
            $('#file_dvalue').hide();
            $('#input_type option[value=""]').attr('selected', 'selected');
            $('#file_ctime').show();
            break;            
    }    
}

function checkLabel(){
    return $('#label_select').val() != '';
}

function checkLabelText(){
    return $('#label_select').val() == '2';
}

function checkDesc(){
    return $('#desc_select').val() != '';
}

function checkDescText(){
    return $('#desc_select').val() == '2';
}
</script>
<?php
$this->endBlock();
echo $this->extend('template'.DS.'form');